//var $WEB_ROOT_PATH = getContextPath();
function getContextPath(){
	var strFullPath=window.document.location.href;
	var strPath=window.document.location.pathname;
	var pos=strFullPath.indexOf(strPath);
	var prePath=strFullPath.substring(0,pos);
	var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	var basePath = prePath;
	basePath = prePath + postPath;
	return basePath;
}

var $WEB_ROOT_PATH = getContext();
function getContext(){
	var pathName = window.location.pathname.substring(1);
	var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
	// if (webName == "") {
	// 	return window.location.protocol + '//' + window.location.host;
	// } else {
	// 	return window.location.protocol + '//' + window.location.host + '/'+ webName;
	// }
	return pathName.substring(0,0); //linux server
}

var $WEB_ROOT_IP=getIpPath();
function getIpPath(){
    var hostport=document.location.host;
    var arr=hostport.split(':');
    var ip=arr[0];
    return ip;
	
}

var $WEB_ROOT_PORT=getPortPath();
function getPortPath(){
    var hostport=document.location.host;
    var arr=hostport.split(':');
    var port=arr[1];
    return port;
}


$.ajaxSetup({  
	cache: false,
	complete: function (XMLHttpRequest, textStatus) {
		var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
		if (sessionstatus == "timeout") {
			if( window.top != window.self ){
				window.top.location = "${pageContext.request.contextPath}";
			}
			else{
				window.location.replace("${pageContext.request.contextPath}");
			}
		}
	}
});

(function($){
    $.fn.serializeJson=function(){
        var serializeObj={};
        var array=this.serializeArray();
        //var str=this.serialize();
        $(array).each(function(){
            if(serializeObj[this.name]){
                if($.isArray(serializeObj[this.name])){
                    serializeObj[this.name].push(this.value);
                }else{
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }
            }else{
                serializeObj[this.name]=this.value;
            }
        });
        return serializeObj;
    };
})(jQuery);

(function($){
    $.fn.serializeJsonStr=function(){
        var serializeObj={};
        var array=this.serializeArray();
        //var str=this.serialize();
        $(array).each(function(){
            if(serializeObj[this.name]){
                if($.isArray(serializeObj[this.name])){
                    serializeObj[this.name].push(this.value);
                }else{
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }
            }else{
                serializeObj[this.name]=this.value;
            }
        });
        return JSON.stringify(serializeObj);
    };
})(jQuery);

(function($){
    $.postJson=function( url, data, callback, type ){
    	// shift arguments if data argument was omitted
		if ( jQuery.isFunction( data ) ) {
			type = type || callback;
			callback = data;
			data = undefined;
		}

		return jQuery.ajax({
			type: 'POST',
			contentType : 'application/json',
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
    };
})(jQuery);

(function($){
    $.postJsonAsync=function( url, data, callback, type ){
    	// shift arguments if data argument was omitted
		if ( jQuery.isFunction( data ) ) {
			type = type || callback;
			callback = data;
			data = undefined;
		}

		return jQuery.ajax({
			type: 'POST',
			async : false,
			contentType : 'application/json',
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
    };
})(jQuery);

function initPagination(p){
	$(p).pagination({  
	    pageSize: 20,//每页显示的记录条数，默认为20  
	    pageList: [20,50,100,200,500,1000],//可以设置每页记录条数的列表  
	    beforePageText: '第',//页数文本框前显示的汉字  
	    afterPageText: '页    共 {pages} 页',  
	    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录', 
	    
	    onBeforeRefresh:function(pageNumber, pageSize){
	        $(this).pagination('loading');
	        $(this).pagination('loaded');
	       }

	});
}

/**
 * js屏蔽backspace键
 */
$(document).keydown(function(e){
	e = window.event || e;
 	var code = e.keyCode || e.which;
 	if (code == 8) {
 		var src = e.srcElement || e.target;
 		var tag = src.tagName;
 		if (tag != "INPUT" && tag != "TEXTAREA") {
 			e.returnValue = false; 
 			return false;
 		} else if ((tag == "INPUT" || tag == "TEXTAREA") && src.readOnly == true) {
 			e.returnValue = false;
 			return false; 
 		}
 	}
});
